<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <ul>
        <li>11111111</li>
        <li>2222222</li>
        <li>3333333</li>
        <li>4444444</li>
        <li>55555555</li>
        <li>66666666</li>
        <li>77777777</li>
        <li>888888888</li>
    </ul>
    <script>
        let lis = document.querySelectorAll('li') //NodeList
        console.log(lis)
        // lis.forEach((element, index) => index % 2 ? element.style.backgroundColor = 'red':element.style.backgroundColor='green')

        let lis1 = document.getElementsByTagName('li') //HTMLCollection
        for (let i = 0; i < lis1.length; i++) {
            // 给每个li上颜色
            // i % 2 ? lis1[i].style.backgroundColor = 'red' : lis1[i].style.backgroundColor = 'green'
            // 给每个li绑定事件
            let currentColor=''
            lis1[i].onmouseover = () => {
                currentColor=lis1[i].style.backgroundColor
                lis1[i].style.backgroundColor = 'red'
            }
            lis1[i].onmouseout = () => {
                lis1[i].style.backgroundColor = currentColor
            }
        }

    </script>
</body>

</html>